const db = require('../database/user')

module.exports = {
  userLogin: async (user) => {
    let dbResult = {}
    try {
      dbResult = await db.userLoginDB(user)
    } catch {
      return Promise.reject()
    }
    return dbResult.length
      ? Promise.resolve(dbResult[0])
      : Promise.reject('用户名或密码错误')
  },
  userRegister: async (user) => {
    let dbResult = {}
    try {
      dbResult = await db.userRegisterDB(user)
    } catch {
      return Promise.reject()
    }
    return dbResult.insertId 
      ? Promise.resolve({ 
          id: dbResult.insertId 
        })
      : Promise.reject()
  },
  userCheck: async (username) => {
    const dbResult = await db.usernameCheckDB(username)
    return dbResult.length 
      ? Promise.reject(dbResult[0])
      : Promise.resolve()
  }
}